import optipngEncode, { OptiPngModule } from '../../lib/optipng/optipng';
import { initEmscriptenModule } from '../util';
import path from 'path';


namespace optipng {


    export const mimeType = 'image/png';


    export const extension = 'png';


    export class EncodeOptions {
        level: number = 1
    };
    


    let emscriptenModule: OptiPngModule;

    const wasmUrl = path.join(__dirname, "../../lib/optipng/optipng.wasm");


    export async function encode(data: Buffer, options?: EncodeOptions): Promise<Uint8Array> {
        if (!emscriptenModule) {
            emscriptenModule = await initEmscriptenModule(optipngEncode, wasmUrl);
        }
        if (!options) {
            options = new EncodeOptions;
        }
        let resultView = emscriptenModule.compress(data, options);
        let result = new Uint8Array(resultView);
        emscriptenModule.free_result();
        return result;
    }

}


export = optipng;